package com.kl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kl.em.ConstantEnum;
import com.kl.em.Result;
import com.kl.entity.TabOrder;
import com.kl.mapper.TabOrderMapper;
import com.kl.service.IOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Wrapper;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2021-10-30
 */
@Service
public class OrderServiceImpl extends ServiceImpl<TabOrderMapper, TabOrder> implements IOrderService {

    @Autowired
    private TabOrderMapper orderMapper;


    @Override
    public void addOrder(TabOrder order) {
        int insert = orderMapper.insert(order);

    }

    @Override
    public Result updateOrder(TabOrder order) {
        int i = orderMapper.updateById(order);
        if(i <= 0){
            return Result.build(ConstantEnum.ERROR);
        }
        return Result.build(ConstantEnum.SUCCESS);
    }

    @Override
    public TabOrder findOrderById(Long id) {
        TabOrder order = orderMapper.selectById(id);
        return order;
    }

    @Override
    public Result updateOrderStatus(TabOrder order) {
        int i = orderMapper.updateById(order);
        if(i <= 0){
            return Result.build(ConstantEnum.ERROR);
        }
        return Result.build(ConstantEnum.SUCCESS);
    }

    @Override
    public Result updateOrderAlyOrderNumber(String serialNumber, String alyOrderNumber) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("aly_order_number",alyOrderNumber);
        TabOrder tabOrder = orderMapper.selectOne(queryWrapper);
        tabOrder.setSerialNumber(serialNumber);
        tabOrder.setPaymentStatus(2);
        tabOrder.setOrderUpdatetime(new Date());
        tabOrder.setTransactionCompletiontime(new Date());
        tabOrder.setClosingTime(new Date());
        int i = orderMapper.updateById(tabOrder);
        if(i <= 0){
            return Result.build(ConstantEnum.ERROR);
        }
        return Result.build(ConstantEnum.SUCCESS);
    }

}
